package com.imagefake.controller;

import com.imagefake.entity.SysUser;
import com.imagefake.service.SysUserService;
import com.imagefake.vo.HttpResult;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * (SysUser)表控制层
 *
 * @author wj
 * @since 2024-03-05 20:09:07
 */
@RestController
@CrossOrigin
@Slf4j
public class SysUserController {
    /**
     * 服务对象
     */
    @Resource
    private SysUserService sysUserService;


    //查询用户列表
    @GetMapping("/list")
    public List<SysUser> list() {
        return sysUserService.list();
    }


    //用户注册
    @PostMapping("/register")
    public HttpResult register(@RequestBody SysUser sysUser) {
        log.info(sysUser.toString());
        if (sysUserService.getByUserName(sysUser.getUsername()) != null) {
            return new HttpResult(-1, "用户名已存在");
        }
        //判断是否插入成功
        if (sysUserService.save(sysUser)) {
            return new HttpResult(200, "注册成功");
        }
        return new HttpResult(-1, "注册失败");
    }
    //查询用户Id

    @GetMapping("/getUserId/{username}")
    public HttpResult getUserIdByUserName(@PathVariable("username") String username){
        SysUser user = sysUserService.getByUserName(username);
        if(user != null){
            return new HttpResult(200,"查询用户Id成功",user.getUserId());
        }
        return new HttpResult(-1,"查询用户Id失败");
    }

//    public HttpResult getUserInfo(String username) {
//        SysUser user = sysUserService.getByUserName(username);
//        if (user != null) {
//            return new HttpResult(200, "查询成功", user);
//        }
//        return new HttpResult(-1, "该用户不存在");
//    }

}

